package de.tum.in.eist.poll.shared;

import java.io.Serializable;
import java.util.ArrayList;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

/**
 * this class represents a user of the system.
 * @author andriy.stefanov@gmail.com
 *
 */
@PersistenceCapable
public class User implements Serializable {
	
	@NotPersistent
	private static final long serialVersionUID = 1L;
	@NotPersistent
	public final static int STUDENT = 0;
	@NotPersistent
	public final static int LECTURER = 1;
	@NotPersistent
	public final static int ADMIN = 2;
	
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    @Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
    private String key;
	
	/**
	 * email address of this user
	 */
	@Persistent
	private String email;
	
	/**
	 * password of this user
	 */
	@Persistent
	private String psswd;
	
	/**
	 * account level of this user
	 */
	@Persistent
	private int accountLevel;
	
	/**
	 * notifications for this user
	 */
	@Persistent
	private ArrayList<String> notificationKeys = new ArrayList<String>();
	
	@NotPersistent
	private ArrayList<Lecture> notifications = new ArrayList<Lecture>();
	
	/**
	 * create empty user object
	 */
	public User(){
		this("","",0);
	}
	
	/**
	 * create new user object.
	 * @param id - id of this user object, given from the database
	 * @param email - email of the new user
	 * @param psswd - password of the new user
	 * @param accLevel - account level of the new user
	 */
	public User(String email, String psswd, int accLevel) {
		this.email = email;
		this.psswd = psswd;
		this.accountLevel = accLevel;
	}
	
	/**
	 * create new user object. user created by this constructor is a student per default.
	 * @param id - id of this user object, given from the database
	 * @param email - email of the new user
	 * @param psswd - password of the new user
	 */
	public User(String email, String pass) {
		this(email, pass, 0);
	}

	/**
	 * Get the email address of this user.
	 * @return email address as String
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * Get the password of this user.
	 * @return password as String
	 */
	public String getPassword() {
		return psswd;
	}

	/**
	 * Get the account level of this user.
	 * @return account level
	 */
	public int getAccountLevel() {
		return accountLevel;
	}
	
	/**
	 * check if this user is a student ({@code accLevel == User.STUDENT}).
	 * @return {@code true} if this user is a student, otherwise {@code false}
	 */
	public boolean isStudent(){
		return accountLevel ==  User.STUDENT;
	}
	
	/**
	 * check if this user is a lecturer ({@code accLevel == User.LECTURER}).
	 * @return {@code true} if this user is a lecturer, otherwise {@code false}
	 */
	public boolean isLecturer(){
		return accountLevel == User.LECTURER;
	}

	@Override
	public boolean equals(Object o){
		if(o instanceof User){
			return ((User)o).key.equals(this.key);
		}
		return false;
	}
	
	public String getKey(){
		return key;
	}
	
	public String toString(){
		return "User {Email:" + email + 
			", Notificationkeys:" + notificationKeys +
			", Notifications:" + notifications;
	}

	public void addNotification(Lecture notification){
		notificationKeys.add(notification.getKey());
		notifications.add(notification);
	}
	
	public ArrayList<Lecture> getNotifications(){
		return notifications;
	}
	
	public void setNotifications(ArrayList<Lecture> notifications){
		this.notifications = notifications;
		for(int i = 0; i < notifications.size(); i++){
			notificationKeys.set(i, notifications.get(i).getKey());
		}
	}
	
	public void clearAllNotifications(){
		notificationKeys.clear();
//		notifications.clear();
	}
	
	public void clearNotification(Lecture lecture){
		for(int i = 0; i < notifications.size(); i++){
			if(notifications.get(i).equals(lecture)){
				notifications.remove(i);
				notificationKeys.remove(i);
				return;
			}
		}
	}
	
	public ArrayList<String> getNotificationKeys() {
		return notificationKeys;
	}

	public void setNotificationKeys(ArrayList<String> notificationKeys) {
		this.notificationKeys = notificationKeys;
	}
}
